<?php
	# MantisConnect - a Dot Net library to connect to Mantis Bug Tracker
	# Copyright (C) 2004-2005  Victor Boctor - vboctor@users.sourceforge.net
	# This program is distributed under dual licensing.  These include
	# GPL and a commercial licenses.  Victor Boctor reserves the right to
	# change the license of future releases.
	# See docs/ folder for more details

	# --------------------------------------------------------
	# $Id: mc_api.php,v 1.15 2005/06/26 00:17:58 vboctor Exp $
	# --------------------------------------------------------

	# use standard error handler rather than the one defined in Mantis.
	restore_error_handler();

	# override some Mantis configurations
	$g_show_detailed_errors	= ON;
	$g_show_notices = ON;
	$g_show_warnings = ON;
	$g_stop_on_errors = ON;

	/**
	 * Get the MantisConnect webservice version.
	 */
	function mc_version()
	{
		return '0.0.5';
	}

	# --------------------
	# Checks if Mantis installation is marked as offline by the administrator.
	# true: offline, false: online
	function mci_is_mantis_offline() {
		$t_offline_file = dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'mantis_offline.php';
		return file_exists( $t_offline_file );
	}

	# --------------------
	# return user_id if successful, otherwise false.
	function mci_check_login( $p_username, $p_password ) {
		if ( mci_is_mantis_offline() ) {
			return false;
		}

		# if no user name supplied, then attempt to login as anonymous user.
		if ( is_blank( $p_username ) ) {
			$t_anon_allowed = config_get( 'allow_anonymous_login' );
			if ( OFF == $t_anon_allowed ) {
				return false;
			}

			$p_username = config_get( 'anonymous_account' );

			# do not use password validation.
			$p_password = null;
		}

		if ( false === auth_attempt_script_login( $p_username, $p_password ) ) {
			return false;
		}

		return auth_get_current_user_id();
	}

	# --------------------
	function mci_has_readonly_access( $p_user_id ) {
		return access_has_global_level( config_get( 'mc_readonly_access_level_threshold' ) );
	}

	# --------------------
	function mci_has_readwrite_access( $p_user_id ) {
		return access_has_global_level( config_get( 'mc_readwrite_access_level_threshold' ) );
	}

	# --------------------
	function mci_get_project_id( $p_project ) {
		if ( (int)$p_project['id'] != 0 ) {
			$t_project_id =  (int)$p_project['id'];
		} else {
			$t_project_id = project_get_id_by_name( $p_project['name'] );
		}

		return $t_project_id;
	}

	# --------------------
	function mci_get_user_id( &$p_user ) {
		if ( !isset( $p_user ) ) {
			return 0;
		}

		$t_user_id = 0;

		if ( (int)$p_user['id'] != 0 ) {
			$t_user_id =  (int)$p_user['id'];
		} else {
			if ( isset( $p_user['name'] ) ) {
				$t_user_id = user_get_id_by_name( $p_user['name'] );
			}
		}

		return $t_user_id;
	}

	# --------------------
	function mci_get_status_id( $p_status ) {
		return mci_get_enum_id_from_objectref( 'status', $p_status );
	}

	# --------------------
	function mci_get_severity_id( $p_severity ) {
		return mci_get_enum_id_from_objectref( 'severity', $p_severity );
	}

	# --------------------
	function mci_get_priority_id( $p_priority ) {
		return mci_get_enum_id_from_objectref( 'priority', $p_priority );
	}

	# --------------------
	function mci_get_reproducibility_id( $p_reproducibility ) {
		return mci_get_enum_id_from_objectref( 'reproducibility', $p_reproducibility );
	}

	# --------------------
	function mci_get_resolution_id( $p_resolution ) {
		return mci_get_enum_id_from_objectref( 'resolution', $p_resolution );
	}

	# --------------------
	function mci_get_projection_id( $p_projection ) {
		return mci_get_enum_id_from_objectref( 'projection', $p_projection );
	}

	# --------------------
	function mci_get_eta_id( $p_eta ) {
		return mci_get_enum_id_from_objectref( 'eta', $p_eta );
	}

	# --------------------
	function mci_get_view_state_id( $p_view_state ) {
		return mci_get_enum_id_from_objectref( 'view_state', $p_view_state );
	}

	# --------------------
	# Get null on empty value.
	#
	# @param Object $p_value  The value
	# @return Object  The value if not empty; null otherwise.
	#
	function mci_null_if_empty( &$p_value ) {
		if( !is_blank( $p_value ) ) {
			return $p_value;
		}

		return null;
	}

	/**
	 * Gets the url for Mantis.  This is based on the 'path' config variable in Mantis.  However,
	 * the default value for 'path' doesn't work properly when access from within MantisConnect.
	 * This internal function fixes this bug.
	 *
	 * @return Mantis URL terminated by a /.
	 */
	function mci_get_mantis_path() {
		$t_path = config_get( 'path' );
		$t_dir = basename( dirname( __FILE__ ) );

		# for some reason str_replace() doesn't work when DIRECTORY_SEPARATOR (/) is in the search
		# string.
		$t_path = str_replace( $t_dir . '/', '', $t_path );

		return $t_path;
	}
?>